<template>
  <div class="icon-generator">
    <h2>iOS Chart - 图表生成器</h2>

    <!-- 标题输入框 -->
    <div class="form-group">
      <label for="title-input">标题:</label>
      <input id="title-input" v-model="title" placeholder="输入标题" class="input-field"/>
    </div>

    <!-- 文件上传 -->
    <div class="form-group">
      <label for="file-input">文件上传:</label>
      <input id="file-input" type="file" @change="handleFileUpload" class="file-input"/>
    </div>

    <!-- 数据输入框 -->
    <div class="form-group">
      <label for="data-textarea">数据:</label>
      <textarea id="data-textarea" v-model="data" placeholder="输入数据" class="textarea-field"></textarea>
    </div>

    <!-- 生成图表按钮 -->
    <div class="button-group">
      <el-button @click="generateChart" type="primary">生成图表</el-button>
    </div>

    <!-- 图表容器 -->
    <div id="chart"></div>

    <!-- 下载按钮 -->
    <div class="button-group">
      <el-button type="primary" @click="downlandVideo">下载视频</el-button>
      <el-button type="primary" @click="downlandGif">下载gif文件</el-button>
    </div>
  </div>
</template>

<script>
// 最初版本的代码
import VMind from '@visactor/vmind';
import {VChart} from "@visactor/vchart";
import {FFmpeg} from "@ffmpeg/ffmpeg";

// 全局变量
const mind = new VMind({
  url: 'https://newapi.xauat.site/v1/chat/completions',
  model: 'gpt-4o-mini',
  headers: {
    Authorization: 'Bearer sk-qqFZNu8Fpg6uOLgjhnEsDXhSuueawhqY8BdKHggMorrualYO',
  }
});

export default {
  data() {
    return {
      title: '',
      data: '',
      spec: '',
      time: ''
    };
  },
  methods: {
    handleFileUpload(event) {
      const file = event.target.files[0];
      if (file) {
        const reader = new FileReader();
        reader.onload = (e) => {
          this.data = e.target.result;
        };
        reader.readAsText(file);
      }
    },
    async generateChart() {
      let fieldInfo, dataset;
      if (this.data.startsWith('{') || this.data.startsWith('[')) {
        dataset = JSON.parse(this.data);
        fieldInfo = mind.getFieldInfo(dataset);
      } else {
        [fieldInfo, dataset] = mind.parseCSVData(this.data);
      }
      let {spec, time} = await mind.generateChart(this.title, fieldInfo, dataset);
      this.spec = spec;
      this.time = time;
      const vChart = new VChart(spec, {dom: 'chart'});
      vChart.renderSync();
    },
    async downlandVideo() {
      const a = document.createElement('a');
      a.href = await mind.exportVideo(this.spec, this.time, {
        VChart: VChart,
        FFmpeg: FFmpeg,
        fetchFile: fetchFile
      });
      a.download = `${this.title}.mp4`;
      a.dispatchEvent(new MouseEvent('click'));
    },
    async downlandGif() {
      const a = document.createElement('a');
      a.href = await mind.exportGIF(this.spec, this.time, {
        VChart: VChart,
        FFmpeg: FFmpeg,
        fetchFile: fetchFile
      });
      a.download = `${this.title}.gif`;
      a.dispatchEvent(new MouseEvent('click'));
    }
  }
};
</script>

<style scoped>
.icon-generator {
  max-width: 480px;
  margin: 0 auto;
  padding: 30px;
  border: 1px solid #e1e1e1;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
h2 {
  text-align: center; 
  margin-bottom: 25px; 
  color: #333; 
  font-size: 24px; 
  font-weight: bold; 
}
.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.input-field,
.textarea-field,
.file-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 16px;
  color: #333;
  box-sizing: border-box;
}

.textarea-field {
  height: 120px;
}

.button-group {
  display: flex;
  gap: 10px;
  justify-content: center;
}

#chart {
  width: 100%;
  height: 400px;
  background-color: #f7f7f7;
  border-radius: 8px;
  border: 1px solid #e1e1e1;
  margin-top: 20px;
}
</style>
